<?php
class formulir extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mformulir');
	}
	
	function index()
	{
		$data['active'] = "formulir";
		$config['base_url'] = base_url().'menu/index/';
		$config['total_rows'] = $this->db->count_all('formulir_begolden');
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;
		
		$config['full_tag_open'] = '<div><ul class="pagination pagination-sm">';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->mformulir->getList($config['per_page'],$this->uri->segment(3));
			$this->load->view('formulir/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add(){
		$data['active'] = "formulir";
		$data['mode'] = "Tambah";
		$data['action'] = "save";
		$this->load->view('formulir/form',$data);
	}
	
	function edit($id_formulir){
		$data['active'] = "formulir";
		$data['mode'] = "Ubah";
		$data['action'] = "update";
		$data['detail'] = $this->mformulir->detail($id_formulir);
		$this->load->view('formulir/form',$data);
	}
	
	function save()
	{
		$id_pendaftar = $this->input->post('id_pendaftar');
		$nama_pendaftar = $this->input->post('nama_pendaftar');
		$no_identitas = $this->input->post('no_identitas');
		$alamat_pendaftar = $this->input->post('alamat_pendaftar');
		$telp_1_pendaftar = $this->input->post('telp_1_pendaftar');
		$telp_2_pendaftar = $this->input->post('telp_2_pendaftar');
		$fax_pendaftar = $this->input->post('fax_pendaftar');
		$email_pendaftar = $this->input->post('email_pendaftar');
		$alamat_lokasi = $this->input->post('alamat_lokasi');
		$telp_lokasi = $this->input->post('telp_lokasi');
		$lebar_lokasi = $this->input->post('lebar_lokasi');
		$panjang_lokasi = $this->input->post('panjang_lokasi');
		$estimasi_anggaran = $this->input->post('estimasi_anggaran');
		$tgl_daftar = date('Y-m-d');
		$secutity_code = $this->input->post('secutity_code');
		$CodeCaptcha = $this->session->userdata('mycaptcha');
		
		$submit = $this->input->post('submit');	
		if($submit && $secutity_code == $CodeCaptcha)
		{
			$config['upload_path'] = './upload/formulir';
			$config['allowed_types'] = 'jpg|png|gif|jpeg|zip|rar';
			$this->load->library('upload', $config);
			$this->upload->do_multi_upload("files");
			$getFile = $this->upload->get_multi_upload_data();
			$upload_pas_foto = $getFile[0]['file_name'];
			$upload_lokasi = $getFile[1]['file_name'];
		
			$this->mformulir->setData($id_pendaftar,$nama_pendaftar,$no_identitas,$alamat_pendaftar,$telp_1_pendaftar,$telp_2_pendaftar,$fax_pendaftar,$email_pendaftar,$alamat_lokasi,$telp_lokasi,$lebar_lokasi,$panjang_lokasi,$estimasi_anggaran,$upload_pas_foto,$upload_lokasi,$tgl_daftar);
			$this->mformulir->create();
			$this->session->set_flashdata('success', true);
			redirect('webindex/content/7/31');
		}
		$this->session->set_flashdata('error', true);
		redirect('webindex/content/7/31');
	}
	
	function update()
	{
		$id_pendaftar = $this->input->post('id_pendaftar');
		$nama_pendaftar = $this->input->post('nama_pendaftar');
		$no_identitas = $this->input->post('no_identitas');
		$alamat_pendaftar = $this->input->post('alamat_pendaftar');
		$telp_1_pendaftar = $this->input->post('telp_1_pendaftar');
		$telp_2_pendaftar = $this->input->post('telp_2_pendaftar');
		$fax_pendaftar = $this->input->post('fax_pendaftar');
		$email_pendaftar = $this->input->post('email_pendaftar');
		$alamat_lokasi = $this->input->post('alamat_lokasi');
		$telp_lokasi = $this->input->post('telp_lokasi');
		$lebar_lokasi = $this->input->post('lebar_lokasi');
		$panjang_lokasi = $this->input->post('panjang_lokasi');
		$estimasi_anggaran = $this->input->post('estimasi_anggaran');
		$upload_pas_foto = $this->input->post('upload_pas_foto');
		$upload_lokasi = $this->input->post('upload_lokasi');
		$tgl_daftar = $this->input->post('tgl_daftar');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mformulir->setData($id_pendaftar,$nama_pendaftar,$no_identitas,$alamat_pendaftar,$telp_1_pendaftar,$telp_2_pendaftar,$fax_pendaftar,$email_pendaftar,$alamat_lokasi,$telp_lokasi,$lebar_lokasi,$panjang_lokasi,$estimasi_anggaran,$upload_pas_foto,$upload_lokasi,$tgl_daftar);
			$this->mformulir->update($id_pendaftar);
			$this->session->set_flashdata('success', true);
			redirect('formulir');
		}
		$this->session->set_flashdata('error', true);
		redirect('formulir');
	}
	
	function downloadDenah($namefile){
		$data = file_get_contents( base_url()."/upload/formulir/".$namefile);
		$name = $namefile;

		force_download($name, $data);
	}
	
	function delete($id_pendaftar)
	{
		$getHapus = $this->mformulir->detail($id_pendaftar);
		@unlink("upload/formulir/".$getHapus[0]['upload_pas_foto']);
		@unlink("upload/formulir/".$getHapus[0]['upload_lokasi']);
		
		if ($this->mformulir->remove($id_pendaftar)){
			$this->session->set_flashdata('delete', true);
			redirect('formulir','refresh');
		}
		$this->session->set_flashdata('error', true);
		redirect('formulir');
	}
	
	function searchData(){
		$data['active'] = "formulir";
		$name = $this->input->post('name');
		$data['results'] = $this->mformulir->getListSearch($name);
		$this->load->view('formulir/list',$data);
	}
}
?>